package com.btpj.hospital_server.controller;

import com.btpj.hospital_server.dto.CommonResult;
import com.btpj.hospital_server.dto.param.UserCaseParam;
import com.btpj.hospital_server.service.HospitalDoctorService;
import com.btpj.hospital_server.service.UserCaseService;
import com.btpj.hospital_server.service.UserMedicalCardService;
import com.btpj.hospital_server.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Api(tags = "用户病例接口")
@RestController
@RequestMapping("/user/case")
public class UserCaseController {

    @Resource
    private UserCaseService userCaseService;

    @Resource
    private UserMedicalCardService userMedicalCardService;

    @Resource
    private HospitalDoctorService hospitalDoctorService;

    @ApiOperation("添加用户病例")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cardId", value = "就诊卡编号", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "appointmentId", value = "预约编号", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "doctorId", value = "医生编号", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "content", value = "病例详情", required = true, dataType = "String", paramType = "query"),
    })
    @PostMapping("/add")
    public CommonResult<Void> addUserCase(UserCaseParam param) {
        Long cardId = param.getCardId();
        if (cardId == null) {
            return CommonResult.error("就诊卡编号不可为空");
        }
        if (!userMedicalCardService.exist(cardId)) {
            return CommonResult.error("该就诊卡编号不存在");
        }

        Long appointmentId = param.getAppointmentId();
        if (appointmentId == null) {
            return CommonResult.error("预约编号不可为空");
        }
        if (!userMedicalCardService.exist(cardId)) {
            return CommonResult.error("预约编号不存在");
        }

        Long doctorId = param.getAppointmentId();
        if (doctorId == null) {
            return CommonResult.error("医生编号不可为空");
        }
        if (!hospitalDoctorService.existDoctor(doctorId)) {
            return CommonResult.error("医生编号不存在");
        }

        if (StringUtil.isEmpty(param.getContent())) {
            return CommonResult.error("病例详情不可为空");
        }

        return CommonResult.error("服务器错误，添加用户病例失败");
    }
}
